<?php
/**
 * 商户端风控操作
 */
namespace Home\Controller;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Library\Http\Request;
use Common\Library\Http\Response;
use Common\Library\SendSMS;
use Home\Model\ConsumeHistoryModel;
use Home\Model\MerchantModel;
use Home\Model\UploadFileModel;
use Think\Controller;
use Think\Log;

class MerchantCROController extends Controller
{
    public function __construct() {
        parent::__construct();
        try {
            Request::parse();
        } catch (BusinessException $e) {
            $ret = ['code' => $e->getCode(), 'msg' => $e->getMessage()];
            $this->ajaxReturn($ret);
        }
    }

    /********************************************************************
     * 功能描述：商家相关证件上传
     * 创建时间：2016-08-05
     * 入参：
     *      {
     *          "merchant_id": "商户ID",
     *          "type": "100商户营业执照，101:店主身份证,102:门店图片",
     *      }
     *      "file": "图片数据流" *
     * 出参：
     *     {
     *          "code": 0,
     *          "msg": "success"
     *     }
     ********************************************************************/
    public function merchantUploadImage()
    {
        try{
            //0.参数校验
            $merchantId = I('post.merchant_id', 0, 'int');
            $type = I('post.type', '', 'string');
            Log::write("商户上传图像：".$merchantId.",".$type,Log::DEBUG);
            if(!$merchantId || !$type){
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }

            //1.商户有效行校验
            $res = D('Merchant')->has($merchantId);
            if(!$res){
                BusinessException::throwException(Constant::MERCHANT_NOT_FOUND);
            }

            if($type == Constant::CERT_TYPE_LOGO){
                //上传门店图片
                $upload = D('UploadFile')->uploadImage('/home/webroot/admin.9maibei.com/data', "/upload/", $merchantId.'_'.$type.'_',2);
                Log::write("结果：".$type.'-'.json_encode($upload)."***".$upload,Log::DEBUG);
                $ret = M('Merchant')->where(['id' => $merchantId])->setField('logo', '/data'.$upload['file']['savepath'].$upload['file']['savename']);
                if(false === $ret || 0 === $ret){
                    BusinessException::throwException(Constant::FAILED);
                }
                $ret = Constant::getReturn(Constant::SUCCESS);
            }

            if($type == Constant::CERT_TYPE_LICENCE || $type == Constant::CERT_TYPE_IDCRAD || $type == Constant::CERT_TYPE_SHOP_ONE || $type == Constant::CERT_TYPE_SHOP_TWO || $type == Constant::CERT_TYPE_CASH  || $type == Constant::CERT_TYPE_COUNTER || $type == Constant::CERT_TYPE_AROUND){
                //上传其他图片
                $upload = D('UploadFile')->uploadImage('/home/webroot/admin.9maibei.com/data', "/upload/", $merchantId.'_'.$type.'_',2);
                Log::write("结果：".$type.'-'.json_encode($upload)."***".$upload,Log::DEBUG);
                if($upload['file']['savename']){
                    $data = [
                        'merchant_id' => $merchantId,
                        'file_name' => $upload['file']['savename'],
                        'file_path' => '/data'.$upload['file']['savepath'],
                        'file_type' => $upload['file']['ext'],
                        'file_size' => $upload['file']['size'],
                        'type' => $type,
                        'status' => Constant::CERT_AUDIT_STATUS_UPLOAD,
                        'add_time' => time(),
                    ];
                    if (false === D('MerchantUploadFile')->add($data)) {
                        BusinessException::throwException(Constant::FAILED);
                    }
                    $ret = Constant::getReturn(Constant::SUCCESS);
                }else{
                    $ret = ['code' => $upload['code'], 'msg' => $upload['msg']];
                }
            }
        }catch (BusinessException $e){
            $ret = ['code' => $e->getCode(), 'msg' => $e->getMessage()];
        }

        Response::format($ret);
        $this->ajaxReturn($ret);
    }
}